<?php
	
	class Admin_CategoryController extends GTL_Action{
		public function init(){
			parent::init();
			/* Initialize action controller here */
            $this->list_sess_unset('controller_category');
            $this->session_search_key = 'controller_category';
            $this->session_search_name = 'search_label';
			
		}
		public function indexAction(){
			$this->_helper->redirector('list','category','admin');
		}
		public function viewAction(){
			if ($this->_getParam('cat_id') && $this->_getParam('cat_id') != '') {
				$cat_id = $this->_getParam('cat_id');
			} else {
				$this->helper->redirector('list','category','admin');
			}
			$categoryModel = new Model_Category();
			$categoryalldata = $categoryModel->fetchEntryById($cat_id);
			$this->view->categoryalldata = $categoryalldata;
		}
		public function listAction(){
			$errorMessage = Array();
			$successmessage = '';
			$successMessage = '';
			$where = 'cat_status != 2) and ( ';
			$searchtype = '';
			$orderField = 'cat_id';
			$sort = 'desc';
			
            /* Pagination Login */
            $itemsPerPageReviews = $this->PAGING_PER_PAGE;
            $currentPageReviews = 1;
			
			 //PAGING_PER_PAGE
            if ($this->_getParam('page') && $this->_getParam('page') != '') {
                $currentPageReviews = $this->_getParam('page');
            }

            if ($this->_getParam('field') && $this->_getParam('field') != '') {
                $orderField = $this->_getParam('field');
            }

            if ($this->_getParam('sort') && $this->_getParam('sort') != '') {
                $sort = $this->_getParam('sort');
            }
			
            $offset = ($currentPageReviews - 1) * $itemsPerPageReviews;
			
			if ($this->_request->isPost()) {
                
                $postedData = $this->_request->getPost();
                //Multiple Delete Logic 
                if (isset($postedData['multiaction']) && ($postedData['multiaction'] != '')) {
                    $action = trim($postedData ['multiaction']);
                    if (isset($postedData['multicheck']) && count($postedData['multicheck']) > 0) {
                        $ids = implode(',', $_POST['multicheck']);
                        $result = $this->deleteAction($_POST['multicheck']);
                    } else {
                        $errorMessage[] = ' Please select atleast one checkbox! ';
                    }
                }
                 
				 
                /*search code here*/
                if ($postedData['searchtype'] == '') {
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
                }
                if (isset($postedData['searchtype']) && ($postedData['searchtype'] != '')) {
                    $searchtype = $postedData['searchtype'];
                    $where .= "cat_name LIKE '%$searchtype%' OR cat_status LIKE '%$searchtype%' ";    
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                    $this->view->searchtype = $searchtype;
			      
                }    
                
            } else {

                if (!isset($_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name])) 
                    {
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = '';
                }
                    $searchtype = $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name];
                    $where .= "cat_name LIKE '%$searchtype%' OR cat_status LIKE '%$searchtype%' ";    
                    $_SESSION['_LISTING_SESS'][$this->session_search_key][$this->session_search_name] = $searchtype;
                    $this->view->searchtype = $searchtype;
            }

			$categoryModel = new Model_Category();
			$categoryData = $categoryModel->fetchCategory($where, $itemsPerPageReviews, $offset, $orderField, $sort);
			
			$paginator = new GTL_Paginator();
            $paginator->setItemsPerPage($itemsPerPageReviews);
            $paginator->setItemsTotal($categoryModel->getCategoryCount($where));
            $paginator->setCurrentPage($currentPageReviews);
            $link = $this->view->url(array('module'=>'admin','controller' => 'category', 'action' => 'list', 'page' => 'PAGENO'), null, false);
            $paginator->paginate($link);

            $this->view->paginator = $paginator;
           
            $this->view->categoryData = $categoryData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $categoryModel->getStatusArray();
            $this->view->searchtype = $searchtype;
			
			
			/*$this->view->categoryData = $categoryData;
            $this->view->errorMessage = $errorMessage;
            $this->view->successmessage = $successmessage;
            $this->view->statusArray = $categoryModel->getStatusArray();
            $this->view->searchtype = $searchtype;*/
		}
		public function deleteAction($ids = Array()) {
            //Load blog Model 
            $categoryModel = new Model_Category();
            if ($this->_getParam('cat_id') && $this->_getParam('cat_id') != '') {
                $cat_id = $this->_getParam('cat_id');
                $categoryModel->deleteCategory($cat_id);
                $this->addSuccessMessage('Record Successfully Deleted');
               $this->_helper->redirector('list', 'category', 'admin');
            } else {
                $categoryModel->deleteCategory($ids);
				$this->addSuccessMessage('Record Successfully Deleted');
                $this->_helper->redirector('list', 'category', 'admin');
            }
        }
		public function formAction(){
		 //Initialize
			 $successmessage = '';
			 $errorMessage = Array();
			 $errorMessageArray =array();
			 $data = array();
			 $categoryData = Array();
			 $this->view->mode = 'Add';
		 
		 //Load blog Model
            $categoryModel = new Model_Category();

        //Load blog Form
           $categoryForm = new Form_Admin_CategoryForm();
		 
		//Load the status array for the select box in form 
            $statusArray = $categoryModel->getStatusArray();
            $categoryForm->cat_status->addMultiOptions($statusArray);
	    
		//Check for the blog Parameter
            $categoryData['cat_id'] = $this->_request->getParam('cat_id');
            $cat_id = (!empty($categoryData['cat_id'])) ? $categoryData['cat_id'] : null;
            if (!empty($cat_id)) {
				//Set the Mode to Edit
				$this->view->mode = 'Edit';
				//Fetch data to be loaded in form 
				$data = $categoryModel->fetchEntryById($cat_id);
            }
			
		//Populate form 
            $this->view->form = $categoryForm->populate($data);
            if ($this->getRequest()->isPost()) {
                //Check for Errors
                $postedData = $this->_request->getPost();
                if (!$categoryForm->isValid($postedData)) {
                    $errorMessage = $categoryForm->getMessages();
                    foreach ($errorMessage as $_err) {
                        foreach ($_err as $_val) {
                            $errorMessageArray[] = $_val;
                        }
                    }
               } else {
                    if (count($errorMessageArray) > 0) {

                    } else {
                     if ($this->view->mode == 'Add') {
                            $categoryData['cat_createddate'] = date('Y-m-d H:i:s');
                        }
                        //blog save
                        $categoryData['cat_id'] = $cat_id;
                        $categoryData['cat_name'] = $postedData['cat_name'];
                       // $userData['blog_createddate'] = $postedData['blog_createddate'];
                        $categoryData['cat_updateddate'] = date('Y-m-d H:i:s');
                        $categoryData['cat_status'] = $postedData['cat_status'];
						

                        $categoryModel->save($categoryData, 'cat_id');
                        if ($this->view->mode == 'Add') {
                            $successmessage = 'Category add successfully';
                        } else {
                            $successmessage = 'Category updated successfully';
                        }
                        $this->addSuccessMessage($successmessage);
                        $this->_helper->redirector('list', 'category', 'admin');
                    }
                }
            }
            $this->view->cat_id = $cat_id;
            $this->view->errorMessageArray = $errorMessageArray;
            $this->view->succesMessage = $successmessage;
            $this->view->form = $categoryForm;
	  }
	}
?>